//
//  ShapeCollection.swift
//  ArchitectGenerics
//
//  Created by 姚胜龙 on 2017/6/27.
//  Copyright © 2017年 姚胜龙. All rights reserved.
//

import UIKit

//图形集合
class ShapeCollection {

    private var  shapeArray:Array<Any>
    init() {
        self.shapeArray = Array<Any>()
        self.shapeArray.append(SquareShape(length: 10))
        self.shapeArray.append(RectangleShape(width: 20, height: 40))
        self.shapeArray.append(CircleShape(redius: 30))
    }
    //总长度
    func caculateTotalLength() -> Float {
        return self.shapeArray.reduce(0, { total, shape in
            var sum = total

            if let square = shape as? SquareShape {
                sum = sum + square.length * 4
            }
            else if let rectangle = shape as? RectangleShape {
                sum = sum + (rectangle.width + rectangle.height) * 2
            }
            else if let circle = shape as? CircleShape {
                sum = sum +  2 * 3.14 * circle.redius
            }
            return sum
        })
    }
    //计算总面积
    func caculateAreas() -> Float {

        return self.shapeArray.reduce(0, { total, shape in
            var sum = total

            if let square = shape as? SquareShape {
                sum = sum + powf(square.length, 2)
            }
            else if let rectangle = shape as? RectangleShape {
                sum = sum + rectangle.width * rectangle.height
            }
            else if let circle = shape as? CircleShape {
                sum = sum +  3.14 * powf(circle.redius, 2)
            }
            return sum
        })
    }
}
